package com.ssh.controller;

import com.ssh.entity.SysUser;
import com.ssh.service.SysUserService;
import com.ssh.util.HttpHelper;
import com.ssh.util.MyTool;
import com.ssh.util.SqlHelper;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Controller// 指明当前类是控制器
@RequestMapping("/sysUser")    // 根路径
public class SysUserController {

    @Autowired// 以类型的方式注入(Spring有多种方式实现自动注入，但按类型注入更简单清晰)
    private SysUserService sysUserService;

    @Autowired
    private SqlHelper sqlHelper;


    /**
     * 【注册】--用户注册
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/register.do")
    public void register(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer=response.getWriter();
        JSONObject resObject = new JSONObject();
        JSONObject json = HttpHelper.getBodyJSON(request, "utf-8");
        try{
            this.sqlHelper.runSql("insert into sys_user (id,userid,password,name,age,registerdate) values(?,?,?,?,?,?)",
                    new Object[]{MyTool.NewUUid(),json.get("userid")+"",json.get("password")+"","郑可","24",new Date()});
            resObject.put("data", true);
        }catch (SQLException e) {
            e.printStackTrace();
            resObject.put("data", false);
        }
        writer.write(resObject.toString());
    }









    @RequestMapping("/getAllSysUser.do")
    public void getAllUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        JSONObject resObject = new JSONObject();

        String id = request.getParameter("id");
        JSONObject jsonTempContract = HttpHelper.getBodyJSON(request, "utf-8");

        List<SysUser> userList = sysUserService.findAll();
        resObject.put("data", userList);
        writer.write(resObject.toString());
    }
    //郑可测试
    @RequestMapping("/checkUser.do")
    public void checkUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter writer = response.getWriter();
        JSONObject resObject = new JSONObject();


        resObject.put("data", "郑可是真的帅！");
        writer.write(resObject.toString());
    }










}